package cn.school.controller.student;


import cn.school.common.api.CommonPage;
import cn.school.common.api.CommonResult;

import cn.school.common.utils.SysUserUtils;
import cn.school.student.entity.TExamPerformance;
import cn.school.student.entity.TExamTestpaper;

import cn.school.student.service.TExamPerformanceService;
import cn.school.student.service.TExamTestpaperService;
import cn.school.student.vo.ChangeFolderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import java.util.Date;
import java.util.List;

/**
 * 考試試卷表(TExamTestpaper)表控制层
 *
 * @author makejava
 * @since 2023-02-16 14:45:33
 */
@RestController
@RequestMapping("tExamTestpaper")
@Api(tags = "TExamTestpaper => 试卷信息")
public class TExamTestpaperController {
    /**
     * 服务对象
     */
    @Autowired
    private TExamTestpaperService tExamTestpaperService;

    @Autowired
    private TExamPerformanceService tExamPerformanceService;


    /**
     * 分页查询
     *
     * @param tExamTestpaper 筛选条件
     * @param pageNum,size   分页对象
     * @return 查询结果
     */
    @GetMapping("/query")
    @ApiOperation("query=>分页查询题目信息")
    public CommonResult<CommonPage<TExamTestpaper>> queryByPage(TExamTestpaper tExamTestpaper, Integer pageNum, Integer pageSize) {
        return CommonResult.success(CommonPage.restPage(this.tExamTestpaperService.queryByPage(tExamTestpaper, PageRequest.of((pageNum - 1), pageSize))));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    @ApiOperation("queryById=>查询单个题目信息")
    public CommonResult<TExamTestpaper> queryById(@PathVariable("id") Long id) {
        return CommonResult.success(this.tExamTestpaperService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param tExamTestpaper 实体
     * @return 新增结果
     */
    @PostMapping("insert")
    @ApiOperation("insert=>新增题目信息")
    public CommonResult<Boolean> add(@RequestBody TExamTestpaper tExamTestpaper) {

        tExamTestpaper.setCreateTime(new Date());
        tExamTestpaper.setCreateBy(SysUserUtils.getSysUser().getUsername());
        Integer insert = this.tExamTestpaperService.insert(tExamTestpaper);


        if (insert != 0) {
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    /**
     * 编辑数据
     *
     * @param tExamTestpaper 实体
     * @return 编辑结果
     */
    @PutMapping("/update")
    @ApiOperation("update=>更新题目信息")
    public CommonResult<Boolean> edit(@RequestBody TExamTestpaper tExamTestpaper) {
        tExamTestpaper.setModifyTime(new Date());
        tExamTestpaper.setModifyBy(SysUserUtils.getSysUser().getUsername());
        int result = this.tExamTestpaperService.update(tExamTestpaper);

        if (result>0) {
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @PostMapping("/delete")
    @ApiOperation("delete=>删除题目信息")
    public CommonResult<Boolean> deleteById(@RequestBody Long id) {
        boolean b = this.tExamTestpaperService.deleteById(id);
        if (b) {
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    /**
     * 查询考试详情
     *
     * @param id
     */
    @GetMapping("/examinationDetails/{id}")
    @ApiOperation("examinationDetails => 试卷详情")
    public CommonResult<TExamTestpaper> examinationDetails(@PathVariable("id") Long id) {
        TExamTestpaper tExamTestpaper = this.tExamTestpaperService.examinationDetails(id, null);

//        TExamTestpaper tExamTestpaper = this.tExamTestpaperService.queryById(id);
//
//
//
//        tExamTestpaper.setExamTestpaperList(tExamTestpaperVos);

        return CommonResult.success(tExamTestpaper);


    }

//    @PostMapping("/queryByGradeId")
//    public CommonResult<List<TExamSubjectVo>> queryByGradeId(Integer[] gradeId) {
//
//
//        QueryWrapper<TExamSubject> tExamSubjectQueryWrapper = new QueryWrapper<>();
//
//        QueryWrapper<TExamCorrelation> tExamSubjectQueryWrapper1 = new QueryWrapper<>();
//
//
//        List<TExamCorrelation> list = new ArrayList<>();
//
//
//        List<List<TExamCorrelation>> cyclist = new ArrayList<>();
//
//        List<List<TExamSubject>> listArrayList = new ArrayList<>();
//
//        Integer gLengh = 0;
//
//        if (gradeId.length == 0) {
//
//            throw new ApiException("班级不能为空！");
//        }
//        for (int i = 0; i < gLengh; i++) {
//            list = tExamCorrelationService.list(tExamSubjectQueryWrapper1);
//            cyclist.add(list);
//        }
//        for (int j = 0; j < tExamCorrelationService.list().size(); j++) {
//            List<TExamSubject> questionList = tExamSubjectService.list(tExamSubjectQueryWrapper.eq("questionsId", list.get(j).getQuestionsId()));
//            listArrayList.add(questionList);
//        }
//
//    }


    @PostMapping("/addTestpaper")
    @ApiOperation("addTestpaper => 添加试卷")
    public CommonResult<Boolean> addTestpaper(@RequestBody TExamTestpaper tExamTestpaper) {

        Integer insert = tExamTestpaperService.insert(tExamTestpaper);

        if (insert != 0) {

            return CommonResult.success();
        }
        return CommonResult.failed();

    }

    @PostMapping("/submitTestpaper")
    @ApiOperation("submitTestpaper=>提交试卷")
    public CommonResult<Integer> submitTestpaper(@RequestBody List<TExamPerformance> tExamPerformances) {
        int num = tExamPerformanceService.submitTestpaper(tExamPerformances);
        if (num != 0) {
            return CommonResult.success();
        }
        if(num==-1){
            return CommonResult.failed("答题为空不能提交");
        }
        return CommonResult.failed();
    }

    @PostMapping("/editFolder")
    @ApiOperation("editFolder=>编辑文件夹")
    public CommonResult<Integer> editFolder(@RequestBody TExamTestpaper tExamTestpaper) {
        int num = tExamTestpaperService.editFolder(tExamTestpaper);
        if (num > 0) {

            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    @PostMapping("/deleteFolder")
    @ApiOperation("deleteFolder=>删除文件夹")
    public CommonResult<Integer> deleteFolder(@RequestBody List<String> ids) {
        int num = tExamTestpaperService.deleteFolder(ids);
        if (num > 0) {
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    @PostMapping("/changeToFolder")
    @ApiOperation("changeToFolder=>添加书卷到文件夹")
    public CommonResult<Integer> changeToFolder(@RequestBody ChangeFolderVO vo) {
        if(vo.getTestPaperList()==null || StringUtils.isEmpty(vo.getPid())){
            return CommonResult.failed("数据错误");
        }
        int num = tExamTestpaperService.changeToFolder(vo);
        if (num > 0) {
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    @PostMapping("/findMysafeAllFolder")
    @ApiOperation("findMysafeAllFolder=>查询自己创建的文件夹")
    public CommonResult<List<TExamTestpaper>> findMysafeAllFolder(@RequestBody ChangeFolderVO vo) {
        List<TExamTestpaper> list = tExamTestpaperService.findMysafeAllFolder();
        return CommonResult.success(list);

    }


}
